$(function(){
    obj.init();
});

var obj = new function() {
    'use strict';
    return {
        inDataGrid: null,
        outDataGrid: null,
        params: {
            type: 'school',
            goal: null,
            goalId: null,
            target: null,
            manage: 0
        },
        dom: {
            save: null,
            btnIn: null,
            btnOut: null,
            all:null
        },
        init: function () {
            this.params.goal    = xhTool.getQuery('goal');
            this.params.goalId  = xhTool.getQuery('goalId');
            this.params.target  = xhTool.getQuery('target');
            this.params.manage  = xhTool.getQuery('manage');

            this.dom.save   = window.top.xhTop.getModalConfirmDom();
            this.dom.btnIn  = $('#bt_in');
            this.dom.btnOut = $('#bt_out');
            this.dom.all    = $('#power_all');

            this.inDataGrid  = new xhDataGrid($('#in_list'), 'Power/getSchoolPower', this.params);
            this.inDataGrid.init().activeBtn(this.dom.btnOut, null, 'bs');
            this.outDataGrid = new xhDataGrid($('#out_list'), 'school/getDgData', {support: 1});
            this.outDataGrid.parseToolbar().init().activeBtn(this.dom.btnIn, null, 'bs');
            this.funObj().dataGridEvent();

            this.listener();
        },
        listener: function () {
            var $this = this;
            //搜索
            xhTool.bindDocKeyDown(
                $('#bt_list_out_search').click(function () {
                    $this.funObj().searchSchool();
                })
            );
            //加入权限
            $this.dom.btnIn.click(function () {
                $this.funObj().addSchoolPower();
            });
            //删除权限
            $this.dom.btnOut.click(function () {
                $this.funObj().delSchoolPower();
            });
            //所有权限
            $this.dom.all.click(function () {
                $this.funObj().dealAllPower($(this).prop('checked'));
            });
            $this.dom.save.click(function () {
                window.top.xhTop.closeModal();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                dataGridEvent: function () {
                    $this.inDataGrid.onLoadSuccess = function (data) {
                        $this.dom.all.prop('checked', data.all);
                    }
                },
                searchSchool: function () {
                    $this.outDataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_out_vague'))
                        }
                    });
                },
                addSchoolPower: function () {
                    var ids = $this.outDataGrid.getSelectValues('iSchoolId');
                    $this.funObj().saveExtraPower(ids, 'add');
                    $this.outDataGrid.clear();
                },
                delSchoolPower: function () {
                    var ids = $this.inDataGrid.getSelectValues('iSchoolId');
                    $this.funObj().saveExtraPower(ids, 'del');
                },
                saveExtraPower: function (ids, operate) {
                    xhExtra.ajaxRequest('power/saveExtraPower', $.extend({}, $this.params, {operate: operate, ids: ids}), {check: true, table: $this.inDataGrid});
                },
                dealAllPower: function (all) {
                    xhExtra.ajaxRequest('power/saveExtraAllPower', $.extend({}, $this.params, {all: all}), {check: true});
                }
            }
        }
    }
};